<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      :headers="headers"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      style="display: inline-block; vertical-align: top"
    >
      <!-- 图片 -->
      <img v-if="value && fileType == eFileType.IMG.code" :src="imgCallBackBaseUrl+value" class="avatar" />
      <!-- 视频 -->
      <video v-if="value && fileType == eFileType.VIDEO.code"
        :src="imgCallBackBaseUrl+value"
        class="avatar video-avatar"
        controls="controls">
        您的浏览器不支持视频播放
      </video>
      <!-- 文件 -->
      <div class="el-upload-list__item-thumbnail" style="padding: 11px 27.5px;" v-if="value && fileType == eFileType.FILE.code">
        <div class="bgImg">
          <div class="content_border">
              <div class="content">{{name}}</div>
          </div>
        </div>
      </div>
      <i v-if="!value" class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { FILE_TYPE } from "@/enum/enum";

export default {
  components: {},
  data() {
    return {
      // 枚举属性
      // 文件类型
      eFileType: FILE_TYPE,
      // 上传图片的服务器地址
      uploadImgUrl: process.env.VUE_APP_STATIC_FILE_URL + "/file/upload",
      // 上传图片的回显地址
      imgCallBackBaseUrl: process.env.VUE_APP_STATIC_FILE_URL,
      headers: {
        token: getToken(),
      },
      // 文件名
      name: "",
      // 文件类型
      fileType: FILE_TYPE.IMG.code
    };
  },
  props: {
    // 文件路径
    value: {
      type: String,
      default: "",
    },
    // 允许文件类型列表
    type: {
      type: Array,
      default: function(){
            return [FILE_TYPE.IMG];
        },
    }
  },
  created() {
    // 设置文件名(去掉'/'前的部分)
    const element = this.value;
    if(element){
      const indexOf = element.lastIndexOf("/")>element.lastIndexOf("\\")?element.lastIndexOf("/"):element.lastIndexOf("\\");
      this.name = element.substring(indexOf+1);
    }
    // 设置文件类型
    this.fileType = this.getFileType(element);
  },
  methods: {
    handleUploadSuccess(res,file,fileList) {
      if(res.code != 200){
        this.loading.close();
        return;
      }
      // 设置文件名
      this.name = file.name;
      // 设置文件类型
      this.fileType = this.getFileType(res.data);
      this.$emit("input", res.data);
      this.loading.close();
    },
    handleBeforeUpload(file) {
      // 如果不包含可以上传的类型就结束
      if(!this.isIncludeTypeAll(this.getFileType(file.name))){
        return false;
      }
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
    // 判断是否包含这个文件的类型
    isIncludeType(code) {
      var isIncludeType = false;
      this.type.forEach(element => {
        if(element.code == code){
          isIncludeType = true;
        }
      });
      return isIncludeType;
    },
    // 判断是否包含这个文件的类型
    isIncludeTypeAll(code) {
      // 通用code
      const codeGeneral = this.eFileType.FILE.code;
      var isIncludeType = false;
      this.type.forEach(element => {
        const elementCode = element.code;
        if(elementCode == code || elementCode == codeGeneral){
          isIncludeType = true;
        }
      });
      return isIncludeType;
    }
  },
  watch: {},
};
</script>

<style scoped lang="scss">
.avatar {
  width: 100%;
  height: 100%;
}
.bgImg {
  width:91px;
  height:122px;
  padding-top:30px;
  padding-right:11.5px;
  padding-left:11.5px;
  padding-bottom:12px;
  background-image: url("../../assets/image/file.png");
  background-size: cover;
}
.content_border {
  position: absolute;
  width: 68px;
  height: 80px;
}
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  width: 68px;
  max-height: 80px;
  line-height: 15px;
  text-align: center;
  color: black;
  overflow: hidden;
  text-overflow:ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 5;
}
</style>